package com.sxhnpg.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.sxhnpg.constant.Constants;
import com.sxhnpg.exception.SxhnpgException;
import com.sxhnpg.manager.TravelManager;
import com.sxhnpg.manager.UserDetailManager;
import com.sxhnpg.manager.UserManager;
import com.sxhnpg.model.TravelAgencyInfo;
import com.sxhnpg.model.User;
import com.sxhnpg.model.UserDetail;
import com.sxhnpg.util.DigestUtil;



public class TravelAction extends BaseAction{

	private String agencyId;
	private String travelAgencyName;
	private String carNumber;
	private String carType;
	private int countPeople;
	private String city;
	private String viewpoint;
	private int isDriver;
	private String driver;
	private String guide;
	private int isContract;
	private Date registerDate;
	private String remainTime;
	private String subsidyPrice;
	private String travelerPrice;
	private String operatePeople;
	private String lastPlace;
	private String mobile2;
	private String payType;
	private String totalPrice;
	private int handle;
	
	//新增
	private String contact;
	private String address;
	private String fax; 
	
	private boolean ok;
	
	// 旅行社管理器
	private TravelManager travelManager;
	
	public void setTravelManager(TravelManager travelManager) {
		this.travelManager = travelManager;
	}
	
	private static Logger logger = Logger.getLogger(TravelAction.class);  
	
	// 旅行社基本信息列表
	public String basicList() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			List<TravelAgencyInfo> travelList = new ArrayList<TravelAgencyInfo>(); 
			travelList = travelManager.findTravelListList();
			ServletActionContext.getRequest().setAttribute("travelList", travelList);
		}catch(Exception e) {
			logger.error("查看旅行社基本信息列表出错：",e);
			throw new SxhnpgException();
		}
		return "list";
	}
	
	// 旅行社基本信息
	public String ajax() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = travelManager.findTravelInfoByName(travelAgencyName);
			if(travelAgencyInfo != null){
				ok = true;
				
				carNumber = travelAgencyInfo.getCarNumber();
				carType = travelAgencyInfo.getCarType();
				countPeople = travelAgencyInfo.getCountPeople();
				city = travelAgencyInfo.getCity();
				viewpoint = travelAgencyInfo.getViewpoint();
				isDriver = travelAgencyInfo.getIsDriver();
				driver = travelAgencyInfo.getDriver();
				guide = travelAgencyInfo.getGuide();
				isContract = travelAgencyInfo.getIsContract();
//				registerDate = travelAgencyInfo.getRegisterDate();
				remainTime = travelAgencyInfo.getRemainTime();
				subsidyPrice = travelAgencyInfo.getSubsidyPrice();
				travelerPrice = travelAgencyInfo.getTravelerPrice();
//				operatePeople = travelAgencyInfo.getOperatePeople();
//				lastPlace;
//				mobile;
				payType = travelAgencyInfo.getPayType();
				totalPrice = travelAgencyInfo.getTotalPrice();
//				handle;
				
				// 新增
				contact = travelAgencyInfo.getContact();
				mobile2 = travelAgencyInfo.getMobile();
				address = travelAgencyInfo.getAddress();
				fax = travelAgencyInfo.getFax();
			}else{
				ok = false;
			}
		}catch(Exception e) {
			logger.error("查看旅行社基本信息列表出错：",e);
			throw new SxhnpgException();
		}
		return "getAjax";
	}
	
	// 检查旅行社信息是否存在
	public String checkBasic() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = travelManager.findTravelInfoByName(travelAgencyName);
			if(travelAgencyInfo != null){
				ok = false;
				
			}else{
				ok = true;
			}
		}catch(Exception e) {
			logger.error("查看旅行社基本信息列表出错：",e);
			throw new SxhnpgException();
		}
		return "getBasicAjax";
	}
	
	// 添加基本信息
	public String addBasic() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = new TravelAgencyInfo();
			travelAgencyInfo.setTravelAgencyName(travelAgencyName);
			travelAgencyInfo.setCarNumber(carNumber);
			travelAgencyInfo.setCarType(carType);
			travelAgencyInfo.setPayType(payType);
			travelAgencyInfo.setCity(city);
			travelAgencyInfo.setViewpoint(viewpoint);
			travelAgencyInfo.setIsDriver(isDriver);
			travelAgencyInfo.setDriver(driver);
			travelAgencyInfo.setGuide(guide);
			travelAgencyInfo.setIsContract(isContract);
			travelAgencyInfo.setRemainTime(remainTime);
			travelAgencyInfo.setCountPeople(countPeople);
			travelAgencyInfo.setSubsidyPrice(subsidyPrice);
			travelAgencyInfo.setTravelerPrice(travelerPrice);
			travelAgencyInfo.setTotalPrice(totalPrice);
			
			travelAgencyInfo.setRegisterDate(new Date());
			travelAgencyInfo.setOperatePeople(userInSession.getUserid());
			travelAgencyInfo.setHandle(0);
			
			// 新增
			travelAgencyInfo.setContact(contact);
			travelAgencyInfo.setMobile(mobile2);
			travelAgencyInfo.setAddress(address);
			travelAgencyInfo.setFax(fax);
			
			travelManager.saveOrUpdate(travelAgencyInfo);
			
			ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "添加基本信息成功！");
		}catch(Exception e) {
			logger.error("添加基本信息出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	// 删除基本信息
	public String removeBasic() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = travelManager.findBasic(agencyId);
			if(travelAgencyInfo != null){
				travelManager.remove(travelAgencyInfo);
				ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "删除基本信息成功！");
			}
		}catch(Exception e) {
			logger.error("删除基本信息出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	// 跳转修改页面
	public String goModifyBasicPage() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = travelManager.findBasic(agencyId);
			
			// 用于进度条判断
//			int progressCount = 0;
//			if(!"".equals(user.getUserDetail().getEmail()) && user.getUserDetail().getEmail() != null){
//				progressCount += 10;
//			}
//			if(!"".equals(user.getUserDetail().getMobile()) && user.getUserDetail().getMobile() != null){
//				progressCount += 10;
//			}
			
//			ServletActionContext.getRequest().setAttribute("progressCount", progressCount);
			ServletActionContext.getRequest().setAttribute("travelAgencyInfo", travelAgencyInfo);
			
		}catch(Exception e) {
			logger.error("跳转修改页面失败：",e);
			throw new SxhnpgException();
		}
		return "modifyPage";
	}
	
	// 修改基本信息
	public String modifyBasic() throws SxhnpgException{
		Map<String, Object> session=(Map<String, Object>)ActionContext.getContext().getSession();
		try{
			User userInSession = (User)session.get(Constants.LOGIN_USER_INFO);
			// 过滤
			if(userInSession == null){
				goLoginPage();
				return null;
			}
			if(userInSession.getPermission() != 1){
				return null;
			}
			TravelAgencyInfo travelAgencyInfo = travelManager.findBasic(agencyId);
			if(travelAgencyInfo != null){
				travelAgencyInfo.setTravelAgencyName(travelAgencyName);
				travelAgencyInfo.setCarNumber(carNumber);
				travelAgencyInfo.setCarType(carType);
				travelAgencyInfo.setPayType(payType);
				travelAgencyInfo.setCity(city);
				travelAgencyInfo.setViewpoint(viewpoint);
				travelAgencyInfo.setIsDriver(isDriver);
				travelAgencyInfo.setDriver(driver);
				travelAgencyInfo.setGuide(guide);
				travelAgencyInfo.setIsContract(isContract);
				travelAgencyInfo.setRemainTime(remainTime);
				travelAgencyInfo.setCountPeople(countPeople);
				travelAgencyInfo.setSubsidyPrice(subsidyPrice);
				travelAgencyInfo.setTravelerPrice(travelerPrice);
				travelAgencyInfo.setTotalPrice(totalPrice);
				
//				travelAgencyInfo.setRegisterDate(new Date());
				travelAgencyInfo.setOperatePeople(userInSession.getUserid());
				travelAgencyInfo.setHandle(0);
				
				// 新增
				travelAgencyInfo.setContact(contact);
				travelAgencyInfo.setMobile(mobile2);
				travelAgencyInfo.setAddress(address);
				travelAgencyInfo.setFax(fax);
				
				travelManager.saveOrUpdate(travelAgencyInfo);
				
				ServletActionContext.getRequest().setAttribute(Constants.OPT_SUCCESS_INFO, "修改基本信息成功！");
			}
		}catch(Exception e) {
			logger.error("修改基本信息出错：",e);
			throw new SxhnpgException();
		}
		return "result";
	}
	
	
	
	
	
	public String getAgencyId() {
		return agencyId;
	}
	public void setAgencyId(String agencyId) {
		this.agencyId = agencyId;
	}
	public String getTravelAgencyName() {
		return travelAgencyName;
	}
	public void setTravelAgencyName(String travelAgencyName) {
		this.travelAgencyName = travelAgencyName;
	}
	public String getCarNumber() {
		return carNumber;
	}
	public void setCarNumber(String carNumber) {
		this.carNumber = carNumber;
	}
	public String getCarType() {
		return carType;
	}
	public void setCarType(String carType) {
		this.carType = carType;
	}
	public int getCountPeople() {
		return countPeople;
	}
	public void setCountPeople(int countPeople) {
		this.countPeople = countPeople;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getViewpoint() {
		return viewpoint;
	}
	public void setViewpoint(String viewpoint) {
		this.viewpoint = viewpoint;
	}
	public int getIsDriver() {
		return isDriver;
	}
	public void setIsDriver(int isDriver) {
		this.isDriver = isDriver;
	}
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getGuide() {
		return guide;
	}
	public void setGuide(String guide) {
		this.guide = guide;
	}
	public int getIsContract() {
		return isContract;
	}
	public void setIsContract(int isContract) {
		this.isContract = isContract;
	}
	public Date getRegisterDate() {
		return registerDate;
	}
	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}
	public String getRemainTime() {
		return remainTime;
	}
	public void setRemainTime(String remainTime) {
		this.remainTime = remainTime;
	}
	public String getSubsidyPrice() {
		return subsidyPrice;
	}
	public void setSubsidyPrice(String subsidyPrice) {
		this.subsidyPrice = subsidyPrice;
	}
	public String getTravelerPrice() {
		return travelerPrice;
	}
	public void setTravelerPrice(String travelerPrice) {
		this.travelerPrice = travelerPrice;
	}
	public String getOperatePeople() {
		return operatePeople;
	}
	public void setOperatePeople(String operatePeople) {
		this.operatePeople = operatePeople;
	}
	public String getLastPlace() {
		return lastPlace;
	}
	public void setLastPlace(String lastPlace) {
		this.lastPlace = lastPlace;
	}
	public String getMobile2() {
		return mobile2;
	}
	public void setMobile2(String mobile2) {
		this.mobile2 = mobile2;
	}
	public String getPayType() {
		return payType;
	}
	public void setPayType(String payType) {
		this.payType = payType;
	}
	public String getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(String totalPrice) {
		this.totalPrice = totalPrice;
	}

	public int getHandle() {
		return handle;
	}
	public void setHandle(int handle) {
		this.handle = handle;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public boolean isOk() {
		return ok;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}
	
}
